package com.hx.dc.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.hx.dc.entity.StoreTable;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

/**
 * Created by Mr.Lee on 2019/3/4.
 */
public class ewm {
    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;
    public static void getEwm (StoreTable storeTable){
        String text = "https://www.baidu.com/"; // 这里是URL ，扫描之后就跳转到这个界面
        String path = "H:\\testSVN_projrect\\hxdcSystem\\web\\tablepic"; // 图片生成的位置
        System.out.println(storeTable.getTablePicWeight()+storeTable.getTablePicHeight());
        int width = storeTable.getTablePicWeight();
        int height = storeTable.getTablePicHeight();
        // 二维码图片格式
        String format =  storeTable.getTablePicPath();
        // 设置编码，防止中文乱码
        Hashtable<EncodeHintType, Object> ht = new Hashtable<EncodeHintType, Object>();
        ht.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        // 设置二维码参数(编码内容，编码类型，图片宽度，图片高度,格式)
        BitMatrix bitMatrix = null;
        try {
            bitMatrix = new MultiFormatWriter().encode(text,
                    BarcodeFormat.QR_CODE, width, height,ht);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        // 生成二维码(定义二维码输出服务器路径)
        File outputFile = new File(path);
        if (!outputFile.exists()) {
            // 创建文件夹
            outputFile.mkdir();
        }
        int b_width = bitMatrix.getWidth();
        int b_height = bitMatrix.getHeight();
        // 建立图像缓冲器
        BufferedImage image = new BufferedImage(b_width, b_height,
                BufferedImage.TYPE_3BYTE_BGR);
        for (int x = 0; x < b_width; x++) {
            for (int y = 0; y < b_height; y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE);
            }
        }
        // 生成二维码
        try {
            ImageIO.write(image, format, new File(path + "/"+storeTable.getTableName()+"." + format)); // 二维码的名称
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 是
        // erweima.sgif
    }
}
